package com.example.petadopt.dao;

import android.annotation.SuppressLint;
import android.content.ContentValues;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.text.TextUtils;

import com.example.petadopt.common.Result;
import com.example.petadopt.entity.Collect;
import com.example.petadopt.entity.Pet;
import com.example.petadopt.utils.SqliteUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * 收藏数据访问对象
 */
@SuppressLint("Range")
public class CollectDao {

    /**
     * 添加收藏记录
     */
    public static Result<Void> addCollect(Collect collect) {
        if (collect == null || collect.getPetId() == null || collect.getCollectUserId() == null || TextUtils.isEmpty(collect.getCollectTime())) {
            return new Result<>(false, "收藏信息不完整", null);
        }

        // 检查宠物是否存在
        Result<Pet> petResult = PetDao.getPetById(collect.getPetId());
        if (!petResult.isSuccess()) {
            return new Result<>(false, "宠物不存在", null);
        }

        ContentValues values = new ContentValues();
        values.put("pet_id", collect.getPetId());
        values.put("collect_time", collect.getCollectTime());
        values.put("collect_user_id", collect.getCollectUserId());

        long i = SqliteUtils.getInstance().getWritableDatabase().insertWithOnConflict("tb_collect", null, values, SQLiteDatabase.CONFLICT_REPLACE);
        if (i > 0) {
            return new Result<>(true, "添加收藏记录成功", null);
        }
        return new Result<>(false, "添加收藏记录失败", null);
    }

    /**
     * 根据用户ID和宠物ID删除收藏记录
     */
    public static Result<Void> deleteCollectByUserAndPet(Long userId, Long petId) {
        if (userId == null || petId == null) {
            return new Result<>(false, "用户ID或宠物ID不能为空", null);
        }

        int rows = SqliteUtils.getInstance().getWritableDatabase().delete("tb_collect", "collect_user_id=? AND pet_id=?", new String[]{String.valueOf(userId), String.valueOf(petId)});
        if (rows > 0) {
            return new Result<>(true, "取消收藏成功", null);
        }
        return new Result<>(false, "取消收藏失败，可能未收藏该宠物", null);
    }

    /**
     * 根据用户ID查询收藏记录
     */
    public static Result<List<Collect>> getCollectsByUserId(Long userId) {
        if (userId == null) {
            return new Result<>(false, "用户ID不能为空", null);
        }
        List<Collect> collectList = new ArrayList<>();
        Cursor cursor = SqliteUtils.getInstance().getReadableDatabase().query("tb_collect", null, "collect_user_id=?", new String[]{String.valueOf(userId)}, null, null, "id DESC");
        while (cursor.moveToNext()) {
            Collect collect = cursorToCollect(cursor);
            collectList.add(collect);
        }
        cursor.close();
        return new Result<>(true, "查询成功", collectList);
    }

    /**
     * 检查用户是否已收藏该宠物
     */
    public static boolean isCollected(Long userId, Long petId) {
        if (userId == null || petId == null) {
            return false;
        }
        Cursor cursor = SqliteUtils.getInstance().getReadableDatabase().query("tb_collect", null, "collect_user_id=? AND pet_id=?", new String[]{String.valueOf(userId), String.valueOf(petId)}, null, null, null);
        boolean collected = cursor.getCount() > 0;
        cursor.close();
        return collected;
    }

    /**
     * 从Cursor提取Collect对象
     */
    private static Collect cursorToCollect(Cursor cursor) {
        Collect collect = new Collect();
        collect.setId(cursor.getLong(cursor.getColumnIndex("id")));
        collect.setPetId(cursor.getLong(cursor.getColumnIndex("pet_id")));
        collect.setCollectTime(cursor.getString(cursor.getColumnIndex("collect_time")));
        collect.setCollectUserId(cursor.getLong(cursor.getColumnIndex("collect_user_id")));
        return collect;
    }
} 